package com.interviews.future;

import com.interviews.common.CommonThreadFactory;

import java.util.Random;
import java.util.concurrent.*;

/**
 * Future的主要功能
 *
 * @author qian
 * @version 1.0
 * @date 2022/3/8 17:09
 */
public class FutureDemo {

    public static void main(String[] args) {
        ExecutorService service = new ThreadPoolExecutor(10, 10, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(), new CommonThreadFactory("FutureDemo"));

        Future<Integer> future = service.submit(new CallableTask());

        try {
            System.out.println(future.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        service.shutdown();


    }

    static class CallableTask implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            TimeUnit.MILLISECONDS.sleep(3000);

            return new Random().nextInt();
        }
    }
}
